HAL DMA initialization and configuration functions migration

The goal of this page is to show how to migrate the Init sequence using STM32CubeMX2:

  • Open STM32CubeMX2 DMA configuration panel.

  • Configure DMA channel request.

  • Configure the main DMA features.

  • Configure the advanced DMA features if needed.

  • Configure the NVIC if the application requires the DMA to be used in IT mode.

  • Generate the code.

DMA memory to memory configuration

Select Channel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Select a DMA channel and assign the Memory to Memory request.

Example Image

Then select software configuration by clicking on wheel_image.

Example Image

HAL DMA memory to memory main features configuration

Example Image

HAL DMA memory to memory advanced features configuration

Configure the advanced DMA features if needed.

DMA advanced feature configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

DMA NVIC configuration

Generated DMA initialization sequence

The DMA initialization sequence is generated in the mx_lpdma1_ch0_init function in the mx_lpdma1_ch0.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : DMA initialization sequence

Topic

HAL1 Code Snippet

HAL2 Code Snippet

Handle declaration

DMA_HandleTypeDef handle_LPDMA1_Channel0;
static hal_dma_handle_t hLPDMA1_CH0;

Initialization and configuration sequence

static void MX_LPDMA1_Init(void)
{

  /* Peripheral clock enable */
  __HAL_RCC_LPDMA1_CLK_ENABLE();

  /* LPDMA1 interrupt Init */
    HAL_NVIC_SetPriority(LPDMA1_Channel0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(LPDMA1_Channel0_IRQn);

  handle_LPDMA1_Channel0.Instance = LPDMA1_Channel0;
  handle_LPDMA1_Channel0.Init.Request = DMA_REQUEST_SW;
  handle_LPDMA1_Channel0.Init.BlkHWRequest = DMA_BREQ_SINGLE_BURST;
  handle_LPDMA1_Channel0.Init.Direction = DMA_MEMORY_TO_MEMORY;
  handle_LPDMA1_Channel0.Init.SrcInc = DMA_SINC_FIXED;
  handle_LPDMA1_Channel0.Init.DestInc = DMA_DINC_FIXED;
  handle_LPDMA1_Channel0.Init.SrcDataWidth = DMA_SRC_DATAWIDTH_BYTE;
  handle_LPDMA1_Channel0.Init.DestDataWidth = DMA_DEST_DATAWIDTH_BYTE;
  handle_LPDMA1_Channel0.Init.Priority = DMA_LOW_PRIORITY_LOW_WEIGHT;
  handle_LPDMA1_Channel0.Init.TransferEventMode = DMA_TCEM_BLOCK_TRANSFER;
  handle_LPDMA1_Channel0.Init.Mode = DMA_NORMAL;
  if (HAL_DMA_Init(&handle_LPDMA1_Channel0) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_DMA_ConfigChannelAttributes(&handle_LPDMA1_Channel0, DMA_CHANNEL_NPRIV) != HAL_OK)
  {
    Error_Handler();
  }

}
hal_dma_handle_t *mx_lpdma1_ch0_init(void)
{
  HAL_RCC_LPDMA1_EnableClock();

  if (HAL_DMA_Init(&hLPDMA1_CH0, HAL_LPDMA1_CH0) != HAL_OK)
  {
    return NULL;
  }

  hal_dma_direct_xfer_config_t xfer_cfg;

  xfer_cfg.request         = HAL_DMA_REQUEST_SW;
  xfer_cfg.direction       = HAL_DMA_DIRECTION_MEMORY_TO_MEMORY;
  xfer_cfg.src_inc         = HAL_DMA_SRC_ADDR_FIXED;
  xfer_cfg.dest_inc        = HAL_DMA_DEST_ADDR_FIXED;
  xfer_cfg.src_data_width  = HAL_DMA_SRC_DATA_WIDTH_BYTE;
  xfer_cfg.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_BYTE;
  xfer_cfg.priority        = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;

  if (HAL_DMA_SetConfigDirectXfer(&hLPDMA1_CH0, &xfer_cfg) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for LPDMA1_CH0 */
  HAL_CORTEX_NVIC_SetPriority(LPDMA1_CH0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(LPDMA1_CH0_IRQn);

  return &hLPDMA1_CH0;
}

Deinitialization sequence

void MX_LPDMA1_DeInit(void)
{
  (void)HAL_DMA_DeInit(&handle_LPDMA1_Channel0);
}
void mx_lpdma1_ch0_deinit(void)
{
  (void)HAL_DMA_DeInit(&hLPDMA1_CH0);
}

IRQ handle function

 void LPDMA1_Channel0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&handle_LPDMA1_Channel0);
}
void LPDMA1_CH0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hLPDMA1_CH0);
}

DMA List configuration

Select Channel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Select a DMA channel and assign the Memory to Memory request.

Example Image

Then select software configuration by clicking on wheel_image.

Example Image

HAL DMA linked list main features configuration

Example Image

NVIC configuration

Configure the NVIC in case of an interrupt.

DMA NVIC configuration

Generated DMA initialization sequence

The DMA initialization sequence is generated in the mx_lpdma1_ch0_init and mx_lpdma1_ch0_deinit functions in the mx_lpdma1_ch0.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : DMA initialization sequence

Topic

HAL1 Code Snippet

HAL2 Code Snippet

Handle declaration

DMA_HandleTypeDef handle_LPDMA1_Channel0;
static hal_dma_handle_t hLPDMA1_CH0;

Initialization and configuration sequence

static void MX_LPDMA1_Init(void)
{

  /* Peripheral clock enable */
  __HAL_RCC_LPDMA1_CLK_ENABLE();

  /* LPDMA1 interrupt Init */
    HAL_NVIC_SetPriority(LPDMA1_Channel0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(LPDMA1_Channel0_IRQn);

  handle_LPDMA1_Channel0.Instance = LPDMA1_Channel0;
  handle_LPDMA1_Channel0.InitLinkedList.Priority = DMA_LOW_PRIORITY_LOW_WEIGHT;
  handle_LPDMA1_Channel0.InitLinkedList.LinkStepMode = DMA_LSM_FULL_EXECUTION;
  handle_LPDMA1_Channel0.InitLinkedList.TransferEventMode = DMA_TCEM_LAST_LL_ITEM_TRANSFER;
  handle_LPDMA1_Channel0.InitLinkedList.LinkedListMode = DMA_LINKEDLIST_NORMAL;
  if (HAL_DMAEx_List_Init(&handle_LPDMA1_Channel0) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_DMA_ConfigChannelAttributes(&handle_LPDMA1_Channel0, DMA_CHANNEL_NPRIV) != HAL_OK)
  {
    Error_Handler();
  }

}
hal_dma_handle_t *mx_lpdma1_ch0_init(void)
{
  HAL_RCC_LPDMA1_EnableClock();

  if (HAL_DMA_Init(&hLPDMA1_CH0, HAL_LPDMA1_CH0) != HAL_OK)
  {
    return NULL;
  }

  /* Set DMA channel linked list xfer configuration */
  hal_dma_linkedlist_xfer_config_t linkedlist_xfer_cfg;
  linkedlist_xfer_cfg.priority        = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;
  linkedlist_xfer_cfg.xfer_event_mode = HAL_DMA_LINKEDLIST_XFER_EVENT_Q;
  if (HAL_DMA_SetConfigLinkedListXfer(&hLPDMA1_CH0, &linkedlist_xfer_cfg) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for LPDMA1_CH0 */
  HAL_CORTEX_NVIC_SetPriority(LPDMA1_CH0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(LPDMA1_CH0_IRQn);

  return &hLPDMA1_CH0;
}

Deinitialization sequence

void MX_LPDMA1_DeInit(void)
{
  (void)HAL_DMA_DeInit(&handle_LPDMA1_Channel0);
}
void mx_lpdma1_ch0_deinit(void)
{
  (void)HAL_DMA_DeInit(&hLPDMA1_CH0);
}

IRQ handle function

 void LPDMA1_Channel0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&handle_LPDMA1_Channel0);
}
void LPDMA1_CH0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hLPDMA1_CH0);
}